avalon.validators.othersIndustry = {
	message: 'The other industry is required and must be less than 50 characters long!',
	get: function(value, field, next) {
		var industry = $("#industry").val();
		if((0 == industry) && (industry != "")) {
			if(value == '') {
				next(false);
			} else {
				var flag = value.length >= 50;
				next(!flag);
			}
		} else {
			next(true);
		}
		return value
	}
}

var editPage = avalon.define({
	$id: "editPage",
	pid: "",
	projectName: "",
	endCustomer: "",
	region: "",
	country: "",
	state: "",
	industry: "",
	endTime: "",
	startTime: "",
	description: "",
	othersIndustry: "",
	estSales: 0,
	chooseIndustry: function() {
		var industry = this.industry;
		if(0 == industry) {
			this.othersIndustry = "";
			$("#others_industry_el").removeClass("mui-hidden");
		} else {
			$("#others_industry_el").addClass("mui-hidden");
		}
	},
	validate: {
		onValidateAll: function(reasons) {
			if(reasons.length) {
				mui.toast(reasons[0].getMessage());
			} else {
				var form = $("#addForm");
				mui.ajax('/project/edit.do', {
					data: form.serialize(),
					dataType: 'json',
					type: 'post',
					success: function(data) {
						mui.alert("", data.msg);
					}
				});

			}
		}

	},
	add: function(e) {
		e.preventDefault()
		this.validate.onManual();
	}
});

mui.ready(function() {

	var pid = $.getParameter("pid");
	mui.ajax('/project/get.do', {
		dataType: 'json',
		type: 'post',
		data: {
			pid: pid
		},
		success: function(data) {
			editPage.pid = pid;
			editPage.projectName = data.name;
			editPage.endCustomer = data.enduser;
			var industry=data.industry;
			editPage.industry = industry;
			
			if("0"==industry){
				editPage.othersIndustry = data.field1;
				$("#others_industry_el").removeClass("mui-hidden");
			}
			
			editPage.description = data.description;
			editPage.estSales = data.estsales;
			editPage.startTime = data.starttime;
			editPage.endTime = data.endtime;
			if(null!=data.region){
				editPage.region = data.region.id;
			}
			if(null!=data.country){
				editPage.country = data.country.id;
			}
			
			if(null!=data.state){
				editPage.state = data.state.id;
			}
			
			
			$("#locations").cxSelect({
				selects: ["region", "country", "state"]
			});

		}
	});

});